# Maintainer: Daniel Bermond <dbermond@archlinux.org>

pkgbase=intel-media-sdk
pkgname=('intel-media-sdk' 'libmfx')
pkgver=23.1.6
pkgrel=1
pkgdesc='Legacy API for hardware video acceleration on Intel GPUs (Broadwell to Rocket Lake)'
arch=('loong64' 'x86_64')
url='https://software.intel.com/en-us/media-sdk/'
license=('MIT')
makedepends=('libdrm' 'libva' 'wayland' 'intel-media-driver'
             'cmake' 'libpciaccess' 'libx11' 'libxcb' 'python')
source=("https://github.com/Intel-Media-SDK/MediaSDK/archive/intel-mediasdk-${pkgver}.tar.gz"
        '010-intel-media-sdk-fix-reproducible-build.patch'::'https://github.com/Intel-Media-SDK/MediaSDK/commit/f6925886f27a39eed2e43c5b7b6c342d00f7a970.patch')
sha256sums=('688d79cfb06cae185a7338a67c8f736657e93a83507cff469037588d8c4cd4d0'
            'f1d8a4edf953cfec1516f1a8383c5ee033245aba16cfae0bc79b7de1a6365fcc')

prepare() {
    patch -d "MediaSDK-intel-mediasdk-${pkgver}" -Np1 -i "${srcdir}/010-intel-media-sdk-fix-reproducible-build.patch"
}

build() {
    export CFLAGS+=' -DNDEBUG -ffat-lto-objects'
    export CXXFLAGS+=' -DNDEBUG -ffat-lto-objects'
    cmake -B build -S "MediaSDK-intel-mediasdk-${pkgver}" \
        -G 'Unix Makefiles' \
        -DBUILD_ALL:BOOL='ON' \
        -DBUILD_TOOLS:BOOL='ON' \
        -DCMAKE_BUILD_TYPE:STRING='None' \
        -DCMAKE_INSTALL_PREFIX:PATH='/usr' \
        -DENABLE_ITT:BOOL='OFF' \
        -DENABLE_OPENCL:BOOL='OFF' \
        -DENABLE_WAYLAND:BOOL='ON' \
        -DENABLE_X11_DRI3:BOOL='ON' \
        -DMFX_APPS_DIR='/usr/lib/mfx' \
        -Wno-dev
    cmake --build build
}

check() {
    ctest --test-dir build --output-on-failure
}

package_intel-media-sdk() {
    depends=('libdrm' 'libva' 'wayland' "libmfx=${pkgver}" 'intel-media-driver')
    options=('!emptydirs')
    provides=('onevpl-runtime')
    
    DESTDIR="$pkgdir" cmake --install build
    ln -s mfx/samples/libcttmetrics.so "${pkgdir}/usr/lib/libcttmetrics.so"
    install -D -m644 "MediaSDK-intel-mediasdk-${pkgver}/LICENSE" -t "${pkgdir}/usr/share/licenses/${pkgname}"
    
    [ -d 'libmfx' ] && rm -rf libmfx
    mkdir -p libmfx/usr/{include,lib/pkgconfig}
    mv "${pkgdir}/usr/include/mfx" libmfx/usr/include
    mv "${pkgdir}/usr/lib"/libmfx.so* libmfx/usr/lib
    mv "${pkgdir}/usr/lib/pkgconfig"/{,lib}mfx.pc libmfx/usr/lib/pkgconfig
}

package_libmfx() {
    pkgdesc='Intel Media SDK dispatcher library'
    depends=('gcc-libs')
    
    mv -T libmfx "$pkgdir"
    install -D -m644 "MediaSDK-intel-mediasdk-${pkgver}/LICENSE" -t "${pkgdir}/usr/share/licenses/${pkgname}"
}
